package com.morehair.controller.admin;


import com.morehair.bean.vo.admin.*;
import com.morehair.service.admin.ConfigService;
import com.morehair.utils.ValidUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * Description: 配置管理
 * Date: 2021/6/7
 * Time: 21:55
 * author: zhang wei
 */
@RestController
@RequestMapping("admin/config")
public class ConfigController {

    @Autowired
    ConfigService configService;


    @GetMapping("mall")
    @RequiresPermissions("admin:config:mall:list")
    public BaseRespVo mallConfigOfGet(){
        //商场配置
        ConfigMallVo configMallVo = configService.queryConfigMall();
        return BaseRespVo.ok(configMallVo);
    }

    @PostMapping("mall")
    @RequiresPermissions("admin:config:mall:updateConfigs")
    public BaseRespVo mallConfigOfPost(@Valid @RequestBody ConfigMallVo configMallVo, BindingResult bindingResult){
        if (bindingResult.hasFieldErrors()) {
            return ValidUtil.valid(bindingResult);
        }
        configService.updateConfigMall(configMallVo);
        return BaseRespVo.ok();
    }


    @GetMapping("express")
    @RequiresPermissions("admin:config:express:list")
    public BaseRespVo expressConfigOfGet(){
        ExpressVo expressVo = configService.queryExpress();
        return BaseRespVo.ok(expressVo);
    }

    @PostMapping("express")
    @RequiresPermissions("admin:config:express:updateConfigs")
    public BaseRespVo express(@Valid@RequestBody ExpressVo expressVo,BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return ValidUtil.valid(bindingResult);
        }
        configService.updateExpress(expressVo);
        return BaseRespVo.ok();
    }



    @GetMapping("order")
    @RequiresPermissions("admin:config:order:list")
    public BaseRespVo order() {
        OrderVo orderVo = configService.queryOrder();
        return BaseRespVo.ok(orderVo);
    }

    @PostMapping("order")
    @RequiresPermissions("admin:config:order:updateConfigs")
    public BaseRespVo order(@Valid@RequestBody OrderVo orderVo,BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return ValidUtil.valid(bindingResult);
        }
        configService.updateOrder(orderVo);
        return BaseRespVo.ok();
    }



    @GetMapping("wx")
    @RequiresPermissions("admin:config:wx:list")
    public BaseRespVo wx() {
        WxVo wxVo = configService.queryWx();
        return BaseRespVo.ok(wxVo);
    }

    @PostMapping("wx")
    @RequiresPermissions("admin:config:wx:Configs")
    public BaseRespVo wx(@Valid@RequestBody WxVo wxVo,BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return ValidUtil.valid(bindingResult);
        }
        configService.updateTx(wxVo);
        return BaseRespVo.ok();
    }

}
